cmake_minimum_required (VERSION 2.8) 
PROJECT(leveldb)
find_package (Threads)
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)
include (CheckFunctionExists)
check_function_exists (fdatasync HAVE_FDATASYNC)

configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_BINARY_DIR}/config.h"
 ) 
 
ENABLE_TESTING()
ADD_SUBDIRECTORY(snappy bin)
 
add_definitions(-DHAVE_CONFIG_H -DSNAPPY)
if(WIN32)
add_definitions(-DUNICODE -D_UNICODE -DLEVELDB_PLATFORM_WIN32 -DOS_WIN -DCOMPILER_MSVC -DNOMINMAX  -D_WIN32_WINNT=0x0600  -D_CRT_SECURE_NO_WARNINGS)
set(PLATFORM_SOURCE  port/port_win32.cc scm/MyDbEnv.cpp scm/MyDbEnv.h scm/MyDbLogger.cpp scm/MyDbLogger.h util/env_null.cc )
elseif(UNIX)
add_definitions(-DLEVELDB_PLATFORM_POSIX)
set(PLATFORM_SOURCE port/port_posix.cc)
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/snappy)
include_directories(${PROJECT_BINARY_DIR})

set(TABLE_SRC_LIST table/block.cc table/merger.h table/block.h table/format.cc table/table.cc table/block_builder.cc table/format.h table/table_builder.cc table/block_builder.h table/iterator.cc  table/filter_block.cc table/iterator_wrapper.h table/two_level_iterator.cc table/filter_block.h table/merger.cc table/two_level_iterator.h)

set(SRC_LIST db/builder.cc   db/c.cc   db/db_impl.cc   db/db_iter.cc   db/filename.cc   db/dbformat.cc   db/log_reader.cc   db/log_writer.cc   db/memtable.cc   db/repair.cc   db/table_cache.cc   db/version_edit.cc   db/version_set.cc   db/write_batch.cc     ${TABLE_SRC_LIST} util/arena.cc   util/cache.cc   util/coding.cc   util/comparator.cc   util/crc32c.cc   util/env.cc   util/hash.cc   util/histogram.cc   util/logging.cc   util/options.cc   util/status.cc util/bloom.cc util/filter_policy.cc ${PLATFORM_SOURCE} )
ADD_LIBRARY(myleveldb STATIC ${SRC_LIST})

LINK_DIRECTORIES(${PROJECT_BINARY_DIR})
add_executable (db_bench db/db_bench.cc util/testutil.cc)
target_link_libraries (db_bench  myleveldb snappy)
target_link_libraries (db_bench  debug aced.lib )
target_link_libraries (db_bench  optimized ace.lib )

add_executable (arena_test util/arena_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (arena_test  myleveldb )
target_link_libraries (arena_test  debug aced.lib )
target_link_libraries (arena_test  optimized ace.lib )
ADD_TEST(arena_test ${CMAKE_CURRENT_BINARY_DIR}/arena_test)

#add_executable (c_test db/c_test.c util/testharness.cc)
#target_link_libraries (c_test  myleveldb )
#target_link_libraries (c_test  debug aced.lib )
#target_link_libraries (c_test  optimized ace.lib )

add_executable (cache_test util/cache_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (cache_test  myleveldb )
target_link_libraries (cache_test  debug aced.lib )
target_link_libraries (cache_test  optimized ace.lib )
ADD_TEST(cache_test ${CMAKE_CURRENT_BINARY_DIR}/cache_test)

add_executable (coding_test util/coding_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (coding_test  myleveldb )
target_link_libraries (coding_test  debug aced.lib )
target_link_libraries (coding_test  optimized ace.lib )
ADD_TEST(coding_test ${CMAKE_CURRENT_BINARY_DIR}/coding_test)

add_executable (corruption_test db/corruption_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (corruption_test  myleveldb snappy)
target_link_libraries (corruption_test  debug aced.lib )
target_link_libraries (corruption_test  optimized ace.lib )
ADD_TEST(corruption_test ${CMAKE_CURRENT_BINARY_DIR}/corruption_test)

add_executable (crc32c_test util/crc32c_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (crc32c_test  myleveldb )
target_link_libraries (crc32c_test  debug aced.lib )
target_link_libraries (crc32c_test  optimized ace.lib )
ADD_TEST(crc32c_test ${CMAKE_CURRENT_BINARY_DIR}/crc32c_test)

add_executable (db_test db/db_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (db_test  myleveldb snappy)
target_link_libraries (db_test  debug aced.lib )
target_link_libraries (db_test  optimized ace.lib )
ADD_TEST(db_test ${CMAKE_CURRENT_BINARY_DIR}/db_test)

add_executable (dbformat_test db/dbformat_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (dbformat_test  myleveldb )
target_link_libraries (dbformat_test  debug aced.lib )
target_link_libraries (dbformat_test  optimized ace.lib )
ADD_TEST(dbformat_test ${CMAKE_CURRENT_BINARY_DIR}/dbformat_test)

add_executable (env_test util/env_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (env_test  myleveldb )
target_link_libraries (env_test  debug aced.lib )
target_link_libraries (env_test  optimized ace.lib )
ADD_TEST(env_test ${CMAKE_CURRENT_BINARY_DIR}/env_test)

add_executable (filename_test db/filename_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (filename_test  myleveldb )
target_link_libraries (filename_test  debug aced.lib )
target_link_libraries (filename_test  optimized ace.lib )
ADD_TEST(filename_test ${CMAKE_CURRENT_BINARY_DIR}/filename_test)

add_executable (log_test db/log_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (log_test  myleveldb )
target_link_libraries (log_test  debug aced.lib )
target_link_libraries (log_test  optimized ace.lib )
ADD_TEST(log_test ${CMAKE_CURRENT_BINARY_DIR}/log_test)

add_executable (table_test table/table_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (table_test  myleveldb snappy)
target_link_libraries (table_test  debug aced.lib )
target_link_libraries (table_test  optimized ace.lib )
ADD_TEST(table_test ${CMAKE_CURRENT_BINARY_DIR}/table_test)

add_executable (skiplist_test db/skiplist_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (skiplist_test  myleveldb )
target_link_libraries (skiplist_test  debug aced.lib )
target_link_libraries (skiplist_test  optimized ace.lib )
ADD_TEST(skiplist_test ${CMAKE_CURRENT_BINARY_DIR}/skiplist_test)

add_executable (version_edit_test db/version_edit_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (version_edit_test  myleveldb snappy)
target_link_libraries (version_edit_test  debug aced.lib )
target_link_libraries (version_edit_test  optimized ace.lib )
ADD_TEST(version_edit_test ${CMAKE_CURRENT_BINARY_DIR}/version_edit_test)

add_executable (version_set_test db/version_set_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (version_set_test  myleveldb snappy)
target_link_libraries (version_set_test  debug aced.lib )
target_link_libraries (version_set_test  optimized ace.lib )
ADD_TEST(version_set_test ${CMAKE_CURRENT_BINARY_DIR}/version_set_test)

add_executable (write_batch_test db/write_batch_test.cc util/testharness.cc util/testutil.cc)
target_link_libraries (write_batch_test  myleveldb )
target_link_libraries (write_batch_test  debug aced.lib )
target_link_libraries (write_batch_test  optimized ace.lib )
ADD_TEST(write_batch_test ${CMAKE_CURRENT_BINARY_DIR}/write_batch_test)
